package com.EventHandler;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.Dao.NewsDao;
import com.Dao.UserDao;
import com.example.myapplication.CommentNewsActivity;
import com.example.myapplication.LikedNewsActivity;
import com.example.myapplication.PostNewsActivity;
import com.example.myapplication.R;
import com.example.myapplication.SetActivity;
import com.news.model.News;

public class BtnEvent {
    //按钮控件 dao 用户id 新闻id
    private Button commentedbtn ;
    private Button likedbtn;
    private Button addDataButton;
    private Button postnewsbtn;
    private Button setAppbtn;
    private UserDao userDao;
    private NewsDao newsDao;
    private View view;
    private Context context;

    //构造函数
    public BtnEvent(View view,Context context){
        this.view = view;
        this.context = context;
        userDao = new UserDao(context);
        newsDao = new NewsDao(context);
    }

    //初始化按钮控件 感觉可以使用抽象类进行复用
    public void initializeUI(){
        commentedbtn = view.findViewById(R.id.commented_btn);
        likedbtn = view.findViewById(R.id.liked_btn);
        addDataButton = view.findViewById(R.id.add_news_btn);
        postnewsbtn = view.findViewById(R.id.post_news_btn);
        setAppbtn = view.findViewById(R.id.set_btn);
        setListeners();
    }

    public void setListeners(){

        //设置点击 查看评论新闻的按钮 监听器
        commentedbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                handleCommented();
            }
        });
        //点赞过新闻/评论 监听器
        likedbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                handleLiked();
            }
        });
        //发布新闻
        postnewsbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {handlePost();}});
        //添加数据
        addDataButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                handleAdd();
            }
        });
        //跳转设置界面
        setAppbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { handleSet(); }
        });
    }

    //处理点击事件 查看评论过的新闻
    public void handleCommented(){
        Intent intent = null;
        intent = new Intent(context, CommentNewsActivity.class);//跳转界面
        context.startActivity(intent);
    }

    //处理点击事件 查看点赞过的评论/新闻
    public void handleLiked(){
        Intent intent = null;
        intent = new Intent(context, LikedNewsActivity.class);//跳转界面
        context.startActivity(intent);
    }
    //处理点击事件 发布新闻
    public void handlePost(){
        Intent intent = null;
        intent = new Intent(context, PostNewsActivity.class);//跳转界面
        context.startActivity(intent);
    }
    //处理点击事件 直接添加新闻数据
    public void handleAdd(){
        Log.d("ButtonClick", "addDataButton 被点击了");
        String title = "周末这些重要消息将影响股市";
        String content="沪深交易所在5月重启，北交所在6月重启。有的上市委会议已经通过了IPO的首发申请。";
        long currentTimeMillis = System.currentTimeMillis();
        News news = new News(title,content,"热点","fjh",currentTimeMillis,null);
        //插入数据
        long flag = newsDao.insertNews(news);
        if(flag==-1){
            Toast.makeText(context, "添加新闻失败！", Toast.LENGTH_SHORT).show();//显示登录成功的弹窗，简单写法
        }
        else{
            Toast.makeText(context, "添加新闻成功！", Toast.LENGTH_SHORT).show();//显示登录成功的弹窗，简单写法
        }
    }
    //处理点击事件 跳转至设置页面
    public  void handleSet(){
        Intent intent = null;
        intent = new Intent(context, SetActivity.class);//跳转界面
        context.startActivity(intent);
    }

}
